<template>
  <div class="sign">
    <div class="sign-head">
      <span>用户注册</span>
    </div>
    <el-form ref="signUpForm" label-width="70px" status-icon :model="registerForm" :rules="SignUpRules">
      <el-form-item prop="username" label="用户名">
        <el-input v-model="registerForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" placeholder="密码" v-model="registerForm.password"></el-input>
      </el-form-item>

      <el-form-item class="sign-btn">
        <el-button type="primary" @click="handleSignUp">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import {defineComponent,reactive,getCurrentInstance} from "vue";
import mixin from "@/mixins/mixin";
import {HttpManager} from "@/api";
import {RouterName,NavName,SignUpRules} from "@/enums";

export default defineComponent({
  setup() {
    const {proxy} = getCurrentInstance();
    const {routerManager,changeIndex} = mixin();
    const registerForm = reactive({
      username: "",
      password: "",

    });
    async function handleSignUp() {
      let canRun = true;
      (proxy.$refs["signUpForm"] as any).validate((valid) => {
        if (!valid) return (canRun = false);
      });
      if (!canRun) return;

      try {
        const username = registerForm.username;
        const password = registerForm.password;
        const result = (await HttpManager.signUp({username,password})) as ResponseBody;
        (proxy as any).$message({
          message: result.message,
          type: result.type,
        });

        if (result.success) {
          changeIndex(NavName.SignIn);
          routerManager(RouterName.SignIn, { path: RouterName.SignIn });
        }
      } catch (error) {
        console.error(error);
      }
    }



    return {
      SignUpRules,
      registerForm,
      handleSignUp
    };
  },
});
</script>

<style lang="scss" scoped>
@import "@/assets/css/sign.scss";
</style>
